<template>
  <section class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前位置</div>
        <ul class="list-btn-ul">
          <li class="list-btn-li">
            <div class="btn">{{this.city}}</div>
          </li>
        </ul>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <ul class="list-btn-ul">
          <li class="list-btn-li"
            v-for="hotCity in hotCities"
            :key="hotCity.id"
            @click="handleCityChange(hotCity.name)">
            <div class="btn">{{hotCity.name}}</div>
          </li>
        </ul>
      </div>
      <div class="area"
        v-for="(city, key) in cities"
        :key="key"
        :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list"
          v-for="(item, index) in city"
          :key="index"
          @click="handleCityChange(item.name)">
          <div class="item border-bottom">{{item.name}}</div>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import BScroll from 'better-scroll'
export default{
  name: 'Home',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    ...mapMutations(['CHANGE_CITY']),
    handleCityChange (city) {
      this.CHANGE_CITY(city)
      this.$router.push('/')
    }
  },
  watch: {
    letter () {
      const element = this.$refs[this.letter][0]
      this.scroll.scrollToElement(element)
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/variable.styl'
.border-topbottom
  &:before
    border-color #ccc
  &:after
    border-color #ccc
.border-topbottom
  &:before
    border-color #ccc
.list
  overflow hidden
  position absolute
  top 1.58rem
  right 0
  bottom 0
  left 0
  .title
    background-color #eee
    line-height .54rem
    padding-left .2rem
    color #666
    font-size .26rem
  .list-btn-ul
    padding .1rem .6rem .1rem .1rem
    overflow hidden
    .list-btn-li
      float left
      width 33.33%
      .btn
        padding .1rem 0
        margin .1rem
        text-align center
        border .02rem solid #ccc
        border-radius .06rem
  .item-list
    .item
      padding-left .2rem
      line-height .8rem
</style>
